export default {
	data() {
		return {
			second: 0,
			minutes: 0,
			duration: 0,
			timeText: '00:00',
			timeDescText: '00:00',
			durationTimeText: '00:00',
			currentTime: 0,
			playStatus: false,
			innerAudioContext: null,
			playOver: false,
			dAudioUrl: '',
			updateTimer: null
		}
	},
	props: {
		audioUrl: {
			type: String,
			default: ''
		}
	},
	computed: {
		position() {
			return this.duration > 0 ? (this.currentTime / this.duration) * 100 : 0;
		}
	},
	watch: {
		audioUrl: {
			handler(v) {
				this.readyAudio(v);
			},
			immediate: true
		},
		dAudioUrl: {
			handler(v) {
				this.readyAudio(v);
			},
			immediate: true
		}
	},
	methods: {
		readyAudio(url) {
			if (url) {
				// 先关掉之前的播放
				if (this.innerAudioContext) {
					this.stopPlay()
					this.stopListenOnTimeUpdate()
					this.timeText = '00:00'
					this.innerAudioContext.destroy()
				}
				if (url.startsWith('/')) {
					url = this.$globalConfig.apiUrl + url;
				}
				this.innerAudioContext = uni.createInnerAudioContext();
				// 为了防止苹果手机静音无法播放
				uni.setInnerAudioOption({
					obeyMuteSwitch: false
				});
				this.innerAudioContext.src = encodeURI(url);
				// 监听
				this.onMonitorEvents();
			}
		},
		changeSlide(value) {
			this.innerAudioContext.seek(value);
			this.innerAudioContext.play();
			this.playStatus = true;
		},
		// 停止播放
		stopPlay() {
			if (this.innerAudioContext) {
				this.playStatus = false;
				this.innerAudioContext.stop();
			}
		},
		// 播放暂停
		playAudio() {
			if (this.innerAudioContext) {
				if (this.playStatus) {
					this.innerAudioContext.pause();
				} else {
					this.innerAudioContext.play();
					this.androidOnTimeUpdate()
				}
				this.playStatus = !this.playStatus;
			}
		},
		calculateTimeLength(v) {
			let minutes = Math.floor(v / 60);
			if (minutes < 10) {
				minutes = '0' + minutes;
			}
			let seconds = Math.floor(v % 60);
			if (seconds < 10) {
				seconds = '0' + seconds;
			}
			return minutes + ':' + seconds;
		},
		// 更新播放时间
		updatePlayTime() {
			this.duration = this.innerAudioContext.duration;
			this.currentTime = this.innerAudioContext.currentTime;
			this.timeText = this.calculateTimeLength(this.currentTime);
			this.durationTimeText = this.calculateTimeLength(this.duration);
			this.timeDescText = this.calculateTimeLength(this.duration - this.currentTime);
		},
		// 监听
		onMonitorEvents() {
			this.innerAudioContext.onCanplay(() => {
				// 必须
				this.innerAudioContext.duration;
				setTimeout(() => {
					this.durationTimeText = this.calculateTimeLength(this.innerAudioContext.duration);
				}, 500)
				console.log('the audio is canPlay......')
			});
			// 播放进度
			if (uni.getSystemInfoSync().platform === 'ios') {
				this.innerAudioContext.onTimeUpdate(() => {
					this.updatePlayTime();
				});
			}
			// 播放暂停
			this.innerAudioContext.onPause(() => {
				this.playStatus = false;
				this.stopListenOnTimeUpdate()
			});
			// 播放停止
			this.innerAudioContext.onStop(() => {
				this.playStatus = false;
				this.stopListenOnTimeUpdate()
			});
			// 播放结束
			this.innerAudioContext.onEnded(() => {
				this.playStatus = false;
				this.currentTime = this.duration;
				this.playOver = true;
				this.updateTimer = null;
				this.stopListenOnTimeUpdate()
				this.$emit('playEnd')
			});
		},
		// 安卓机型的播放状态监听
		androidOnTimeUpdate() {
			if (uni.getSystemInfoSync().platform !== 'ios') {
				this.updateTimer = setInterval(() => {
					this.updatePlayTime();
				}, 1000)
			}
		},
		stopListenOnTimeUpdate() {
			if (this.updateTimer) {
				clearInterval(this.updateTimer)
				this.updateTimer = null
			}
		}
	},
	destroyed() {
		this.stopPlay();
		//销毁这个实例
		if (this.innerAudioContext) {
			this.innerAudioContext.destroy()
		}
		this.stopListenOnTimeUpdate()
	}
}
